﻿using DataAccess;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NLog;

namespace QuanLyKhachSan
{
    public partial class frmThongTinChiTietHoaDonThuePhong : Form
    {
        int mahd;
        bool them = false;
        public bool themthanhcong = false, capnhatthanhcong = false;
        public ChiTietHoaDonThuePhong cthdtp = null;
        ChiTietHoaDonThuePhongManager controller = new ChiTietHoaDonThuePhongManager();
        protected static Logger _logger = LogManager.GetCurrentClassLogger();

        public frmThongTinChiTietHoaDonThuePhong(int mahd,bool them)
        {
            InitializeComponent();
            this.mahd = mahd;
            cthdtp = new ChiTietHoaDonThuePhong();
            this.them = them;
        }

        public frmThongTinChiTietHoaDonThuePhong(ChiTietHoaDonThuePhong cthdtp)
        {
            InitializeComponent();
            this.cthdtp = cthdtp;
        }

        private void bthThoat_Click(object sender, EventArgs e)
        {
            themthanhcong = false; capnhatthanhcong = false;
            this.Close();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            Loading();
        }

        private void Loading()
        {
            if (them)
            {
                txtMaHD.Text = mahd + "";
                txtMaHD.Enabled = false;
                cbKhachHang.SelectedIndex = -1;
                cbPhong.SelectedIndex = -1;
            }
            else
            {
                txtMaHD.Text = cthdtp.MaHD + "";
                txtMaHD.Enabled = false;
                cbKhachHang.SelectedValue = cthdtp.MaKH;
                cbPhong.SelectedValue = cthdtp.SoPhong;
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (them)
            {
                ThemChiTietHoaDonThuePhong();
            }
            
        }

        private void ThemChiTietHoaDonThuePhong()
        {
            cthdtp.MaHD = int.Parse(txtMaHD.Text);
            cthdtp.MaKH = cbKhachHang.SelectedValue + "";
            cthdtp.SoPhong = cbPhong.SelectedValue + "";


            var vResult = controller.Validate(cthdtp);

            if (!vResult.Message.Any())
            {
                var aResult = controller.Them(cthdtp);
                if (!aResult.Exceptions.Any())
                {
                    MessageBox.Show("Thêm thành công chi tiết hóa đơn");
                    themthanhcong = true;
                    _logger.Info("Thêm thành công chi tiết hóa đơn");
                    this.Close();
                }
                else
                {
                    string s = "";
                    foreach (var ex in aResult.Exceptions)
                    {
                        s += ex.Message + "\n";
                    }
                    MessageBox.Show(s);
                    _logger.Error(s);
                }
            }
            else
            {
                string s = "";
                foreach (var ex in vResult.Message)
                {
                    s += ex + "\n";
                }
                MessageBox.Show(s);
                _logger.Error(s);
            }
        }

        private void frmThongTinChiTietHoaDonThuePhong_Load(object sender, EventArgs e)
        {
            this.phongTableAdapter.Fill(this.qLKHACHSANDataSet.Phong);
            this.khachHangTableAdapter.Fill(this.qLKHACHSANDataSet.KhachHang);
            Loading();
        }
    }
}
